﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace System.Workflow.ComponentModel.Compiler
    <Serializable> _
    Public NotInheritable Class ValidationErrorCollection
        Inherits Collection([Of] ValidationError)
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal value As IEnumerable([Of] ValidationError))
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Me.AddRange(value)
        End Sub

        Public Sub New(ByVal value As ValidationErrorCollection)
            Me.AddRange(value)
        End Sub

        Public Sub AddRange(ByVal value As IEnumerable([Of] ValidationError))
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim error As ValidationError
            For Each error In value
                MyBase.Add([error])
            Next
        End Sub

        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As ValidationError)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            MyBase.InsertItem(index, item)
        End Sub

        Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As ValidationError)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            MyBase.SetItem(index, item)
        End Sub

        Public Function ToArray() As ValidationError()
            Dim array As ValidationError() = New ValidationError(MyBase.Count  - 1) {}
            MyBase.CopyTo(array, 0)
            Return array
        End Function


        ' Properties
        Public ReadOnly Property HasErrors As Boolean
            Get
                If (MyBase.Count > 0) Then
                    Dim error As ValidationError
                    For Each error In Me
                        If ((Not [error] Is Nothing) AndAlso Not [error].IsWarning) Then
                            Return True
                        End If
                    Next
                End If
                Return False
            End Get
        End Property

        Public ReadOnly Property HasWarnings As Boolean
            Get
                If (MyBase.Count > 0) Then
                    Dim error As ValidationError
                    For Each error In Me
                        If ((Not [error] Is Nothing) AndAlso [error].IsWarning) Then
                            Return True
                        End If
                    Next
                End If
                Return False
            End Get
        End Property

    End Class
End Namespace

